﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataProcessing
{
    public class ValueRange
    {
        public uint Start
        {
            get;set;
        }

        public uint End
        {
            get;set;
        }
        public uint Size
        {
            get
            { return End - Start + 1; }
        }


        public ValueRange Clone()
        {
            ValueRange ret = new ValueRange();
            ret.Start = Start;
            ret.End = End;
            return ret;
        }
        public bool Contains(uint value)
        {
            if (End >= value && Start <= value)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 求重叠长度
        /// </summary>
        /// <param name="anotherRange"></param>
        /// <returns></returns>
        public uint OverlappedLength(ValueRange anotherRange)
        {
            if (!Contains(anotherRange.Start) && !anotherRange.Contains(Start))
            {
                return 0;
            }
            else
            {
                uint start = Math.Max(anotherRange.Start, Start);
                uint end = Math.Min(anotherRange.End, End);

                return end - start;
            }
        }
        public ValueRange Intersect(ValueRange anotherRange)
        {
            if (!Contains(anotherRange.Start) && !anotherRange.Contains(Start))
            {
                return null;
            }
            else
            {
                uint start = Math.Max(anotherRange.Start, Start);
                uint end = Math.Min(anotherRange.End, End);

                return new ValueRange { Start=start,End=end};
            }
        }
        public bool Equals(ValueRange anotherRange)
        {
            return Start == anotherRange.Start&&End==anotherRange.End;

        }
   
        public void Truncate(ValueRange bound)
        {
            Start = (uint)Util.Truncate((int)(bound.Start), (int)(bound.End), (int)Start);
            End = (uint)Util.Truncate((int)(bound.Start), (int)(bound.End), (int)End);

            return;
        }
        

        public ValueRange Scale(double ratio,double center)
        {
            if(!Contains((uint)center))
                return this.MemberwiseClone() as ValueRange;

            double leftArm = Start- center;
            double rightArm = End -center;


            ValueRange ret = new ValueRange();
            ret.Start = (uint)(center+ leftArm *ratio);
            ret.End = (uint)(center+ rightArm* ratio);
            return ret;
        }

        public ValueRange Add(int offset)
        {
            ValueRange newRange = new ValueRange();


            newRange.Start = EnsurePositiveResult(Start, offset);
            newRange.End =  EnsurePositiveResult(End, offset);
            return newRange;

        }
        static  uint EnsurePositiveResult(uint value,int offset)
        {
            int tmp =(int) value;

            return tmp + offset >= 0 ? (uint)(value + offset) : 0;

        }

    }
}
